﻿// Copyright (c) Microsoft Corporation
//
// All rights reserved. 
//
// Licensed under the Apache License, Version 2.0 (the ""License""); you may not use this file 
// except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING 
// WITHOUT LIMITATION ANY IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, MERCHANTABLITY 
// OR NON-INFRINGEMENT. 
//
// See the Apache Version 2.0 License for specific language governing permissions and limitations under the License.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TED.WindowsAzure.Unplugged.Mobile.Sync.File;
using TED.WindowsAzure.Unplugged.Mobile.Sync.Storage;

namespace TED.WindowsAzure.Unplugged.Mobile.Tests.Mocks
{
    public class MockLocalFileStateStorage : ISyncFileStateStorage
    {
        private readonly Dictionary<int, ISyncFileState> _storage = new Dictionary<int, ISyncFileState>();

        public MockLocalFileStateStorage()
        {
            _storage = new Dictionary<int, ISyncFileState>();

            for (int i = 0; i < 5; i++)
            {
                var fileState = new MockLocalFileState()
                {
                    Id = i,
                    FileName = i.ToString(),
                    FolderName = "foo",
                    Status = SyncFileStatus.AsServer
                };

                _storage.Add(i, fileState);
            }

        }

        public Task<ISyncFileState> CreateFileAsync()
        {
            return Task.Factory.StartNew(() =>
                {
                    return new MockLocalFileState() as ISyncFileState;
                });
        }

        public Task<IEnumerable<ISyncFileState>> ReadAsync(string folderName)
        {
            return Task.Factory.StartNew(() =>
            {
                return _storage.Values.Where(s => s.FolderName.Equals(folderName, StringComparison.CurrentCultureIgnoreCase)).AsEnumerable<ISyncFileState>();
            });
        }

        public Task<ISyncFileState> LookupAsync(int id)
        {
            return Task.Factory.StartNew(() =>
            {
                return _storage[id];
            });
        }

        public Task<ISyncFileState> LookupAsync(string name)
        {
            return Task.Factory.StartNew(() =>
            {
                var file = _storage.Values.Where(l => l.FileName.Equals(name, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault<ISyncFileState>();

                return file;
            });
        }

        public Task InsertAsync(ISyncFileState instance)
        {
            return Task.Factory.StartNew(() =>
            {
                this._storage.Add(int.Parse(instance.FileName), instance);
            });
        }

        public Task UpdateAsync(ISyncFileState instance)
        {
            return Task.Factory.StartNew(() =>
                {
                    _storage[instance.Id] = instance;
                });
        }

        public Task HardDeleteAsync(ISyncFileState instance)
        {
            return Task.Factory.StartNew(() =>
                {
                    _storage.Remove(instance.Id);
                });
        }

        public Task TruncateAsync(string folderName)
        {
            return Task.Factory.StartNew(() =>
            {
                _storage.Clear();
            });
        }

        public Task<IEnumerable<ISyncFileState>> ReadAsync(string folderName, SyncFileStatus status)
        {
            throw new NotImplementedException();
        }

        public Task OpenWriteAsync(ISyncFileState instance)
        {
            throw new NotImplementedException();
        }

        public Task<ISyncFileState> LookupAsync(string folderName, string name)
        {
            throw new NotImplementedException();
        }

        public Task DeleteAsync(ISyncFileState instance)
        {
            throw new NotImplementedException();
        }

        public Task CancelAsync(string folderName)
        {
            throw new NotImplementedException();
        }

        public Task UndoAsync(ISyncFileState instance)
        {
            throw new NotImplementedException();
        }

        public Task UpdateAsync(ISyncFileState instance, SyncFileStatus status)
        {
            throw new NotImplementedException();
        }

        public Task DeleteAsync(ISyncFileState instance, DeleteMode mode = DeleteMode.Soft)
        {
            throw new NotImplementedException();
        }
    }
}
